// components/goods/goodsTabs/index.js
Component({

  externalClasses: ['classify-class'],

  /**
   * 组件的属性列表
   */
  properties: {
    classify: Array,
    sort: Array,
    isShowSwitch: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    goodsTabsActive: '', // tabs active
    switchChecked: false, // switch 是否选中
    isDefaultSort: true, // 默认排序
    sortType: null, // 排序类型：升序/降序
    sortKey: '', // 排序 key
    classifyTabs: '',
    sortVal: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // tabs onChange
    onChange (event) {
      let index = event.detail.name
      this.setData({
        classifyTabs: this.properties.classify[index] && this.properties.classify[index].opt_id
      })
      this.triggerEvent('classifySort', {
        classify: this.data.classifyTabs,
        sort: this.data.sortVal,
        switch: this.data.switchChecked
      }, {})
    },

    // 排序
    onSort (event) {
      let { sortkey, sortup, sortdown, sortdefault } = event.currentTarget.dataset
      if (this.data.sortKey === sortkey && this.data.sortType === 'sortUp') {
        this.setData({
          sortType: 'sortDown',
          sortKey: sortkey,
          sortVal: sortdown,
          isDefaultSort: false
        })
      } else {
        this.setData({
          sortType: 'sortUp',
          sortKey: sortkey,
          sortVal: sortup,
          isDefaultSort: false
        })
      }
      if (sortdefault) {
        this.setData({
          sortType: null,
          isDefaultSort: true
        })
      }
      this.triggerEvent('classifySort', {
        classify: this.data.classifyTabs,
        sort: this.data.sortVal,
        switch: this.data.switchChecked
      }, {})
    },

    // switch 开关
    onSwitchChange ({detail}) {
      this.setData({ switchChecked: detail });
      this.triggerEvent('classifySort', {
        classify: this.data.classifyTabs,
        sort: this.data.sortVal,
        switch: this.data.switchChecked
      }, {})
    }
  }
})
